import { getview } from '@/services/ant-design-pro/view';
import { viewListType } from '@/types/modules/View';
import { Effect, Reducer } from 'umi';
export interface ViewState {
    viewList: viewListType[]
}

export interface IndexModelType {
    namespace: 'views';
    state: ViewState;
    effects: {
        view: Effect,
    };
    reducers: {
        save: Reducer<ViewState>
        // 启用 immer 之后
        // save: ImmerReducer<IndexModelState>;
    };
}

const IndexModel: IndexModelType = {
    namespace: 'views',
    state: {
        viewList: []
    },

    effects: {
        // 初始化
        *view({ payload }, { call, put }) {
            const result = yield call(getview, payload);
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        viewList: result.data[0],
                    }
                })
            }
        },
    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
    },
};

export default IndexModel;